package com.oc.mba.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.oc.mba.dao.BaseDAOHibernate;
import com.oc.mba.dao.DevicePaperDAO;
import com.oc.mba.entity.OcDevicePaper;
import com.oc.mba.util.CommLog;

@Repository
public class DevicePaperDAOImpl extends BaseDAOHibernate implements
		DevicePaperDAO {

	private static final CommLog logger = new CommLog(DevicePaperDAOImpl.class);

	public List<OcDevicePaper> findByProperty(String propertyName, Object value) {
		logger.debug("finding OcDevicePaper instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from OcDevicePaper as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			logger.error("find by property name failed", re);
			throw re;
		}
	}
	
	@Override
	public OcDevicePaper findByID(Integer id) {
		List<OcDevicePaper> paperList = findByProperty("id", id);
		if (paperList!=null && paperList.size()>0) {
			return paperList.get(0);
		}
		return null;
	}
	
	@Override
	public void save(OcDevicePaper devicePaper) {
		getHibernateTemplate().save(devicePaper);
	}
	
	@Override
	public void delete(OcDevicePaper devicePaper) {
		getHibernateTemplate().delete(devicePaper);
	}

	@Override
	public List<OcDevicePaper> findByDeviceID(Integer deviceID) {
		return findByProperty("deviceId", deviceID);
	}

}
